#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/indev/evdev.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>

#include "./../../inc/homepage.h"
#include "./../../inc/msg_popup.h"
#include "./../../inc/cJSON.h"
#include "./../../inc/user_event.h"

#define ULGRAY 0xF5F5F5
#define LDWHITE 0xF6F6F6
// #define LDWHITE 0xFDFDFD
#define FRIEND_ITEM_STEP 43 // 好友列表单个item的高度

lv_obj_t * friend_list_box = NULL;

static void lv_friend_list_show_single_item(lv_obj_t * parent, const char * friend_name, int index);
static void lv_friend_list_show_single_item_event_cb(lv_event_t * e);

void friend_box_init(lv_obj_t * parent)
{
    lv_obj_t * friend_box = lv_obj_create(parent);
    lv_obj_set_size(friend_box, LV_PCT(22), LV_VER_RES);
    lv_obj_set_pos(friend_box, LV_PCT(11), 0);
    lv_obj_set_style_radius(friend_box, 0, 0);
    lv_obj_set_style_border_width(friend_box, 0, 0);
    lv_obj_set_style_pad_all(friend_box, 0, 0);

    // 聊天室频道
    chat_room_init(friend_box);

    // 好友列表
    friend_list_box = lv_obj_create(friend_box);
    lv_obj_set_size(friend_list_box, LV_PCT(100), LV_PCT(90));
    lv_obj_set_style_radius(friend_list_box, 0, 0);
    lv_obj_set_style_border_width(friend_list_box, 0, 0);
    lv_obj_set_pos(friend_list_box, 0, LV_PCT(10));
    lv_obj_set_style_pad_all(friend_list_box, 0, 0);
}

void show_friend_list(lv_obj_t * parent, FriendList_t * p_friendList)
{
    if(p_friendList == NULL) return;
    if(parent == NULL || !lv_obj_is_valid(parent)) {
        printf("父容器无效，无法显示好友列表\n");
        return;
    }

    if(p_friendList->friend_count > 0 && p_friendList->friends != NULL) {
        printf("成功解析到 %d 个好友\n", p_friendList->friend_count);
        lv_obj_clean(parent);

        // 遍历好友数组时，传递当前索引i
        for(int i = 0; i < p_friendList->friend_count; i++) {
            printf("好友 %d: %s\n", i, p_friendList->friends[i].friend_userid);
            lv_friend_list_show_single_item(parent, p_friendList->friends[i].friend_userid, i);
        }
    }

    // 释放friends数组内存
    if(p_friendList->friends != NULL) {
        free(p_friendList->friends);
        p_friendList->friends      = NULL;
        p_friendList->friend_count = 0;
    }
}
static void lv_friend_list_show_single_item(lv_obj_t * parent, const char * friend_name, int index)
{
    if(friend_name == NULL || strlen(friend_name) == 0) {
        return;
    }

    // 创建好友项
    lv_obj_t * friend_item = lv_obj_create(parent);
    lv_obj_set_size(friend_item, LV_PCT(100), LV_PCT(10));
    lv_obj_set_style_radius(friend_item, 0, 0);
    lv_obj_set_style_pad_all(friend_item, 0, 0);
    lv_obj_set_style_bg_color(friend_item, lv_color_hex(WHITE), 0);
    lv_obj_set_style_bg_opa(friend_item, LV_OPA_COVER, 0);
    lv_obj_set_style_border_width(friend_item, 1, 0);
    lv_obj_set_style_border_color(friend_item, lv_color_hex(WHITE), 0);
    lv_obj_set_y(friend_item, index * FRIEND_ITEM_STEP);

    lv_obj_set_user_data(friend_item, (void *)friend_name);

    // 创建好友名称标签
    lv_obj_t * friend_name_title = lv_label_create(friend_item);
    lv_obj_set_style_text_font(friend_name_title, &SourceHanSansCN_Bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(friend_name_title, lv_color_hex(BLACK), 0);
    lv_obj_center(friend_name_title);
    lv_label_set_text(friend_name_title, friend_name);

    // 添加下划线
    lv_obj_t * underline = lv_obj_create(friend_item);
    lv_obj_set_pos(underline, 0, LV_PCT(100) - 1);
    lv_obj_set_size(underline, LV_PCT(100), 1);
    lv_obj_set_style_bg_color(underline, lv_color_hex(ULGRAY), 0);
    lv_obj_set_style_radius(underline, 0, 0);
    lv_obj_set_style_border_width(underline, 0, 0);

    // 添加事件回调
    lv_obj_add_event_cb(friend_item, lv_friend_list_show_single_item_event_cb, LV_EVENT_CLICKED, NULL);
}

static void lv_friend_list_show_single_item_event_cb(lv_event_t * e)
{
    lv_obj_t * clicked_box      = lv_event_get_target(e);
    const char * friend_user_id = (const char *)lv_obj_get_user_data(clicked_box);

    // 检查有效性
    if(friend_user_id == NULL || strlen(friend_user_id) == 0 || clicked_box == NULL) {
        printf("friend_user_id 无效\n");
        return;
    }

    if(strcmp(user_event.type, friend_user_id) == 0) {
        return;
    }

    if(user_event.type_friend_item != NULL && lv_obj_is_valid(user_event.type_friend_item)) {
        lv_obj_set_style_bg_color(user_event.type_friend_item, lv_color_hex(WHITE), 0);
    }

    strncpy(user_event.target_id, friend_user_id, sizeof(user_event.target_id) - 1);
    user_event.target_id[sizeof(user_event.target_id) - 1] = '\0';

    user_event.type_friend_item = clicked_box;
    lv_obj_set_style_bg_color(clicked_box, lv_color_hex(ULGRAY), 0);

    // 更新聊天窗口
    strncpy(user_event.type, friend_user_id, sizeof(user_event.type) - 1);
    user_event.type[sizeof(user_event.type) - 1] = '\0';

    create_chat_box(home_src);
    if(char_box_title != NULL && lv_obj_is_valid(char_box_title)) {
        lv_label_set_text(char_box_title, friend_user_id);
    }
}
